package com.leetcode;

/**
 * 209. 长度最小的子数组
 * 滑动窗口
 */
public class Solution209 {

    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int p1 = 0;
        int p2 = 0;
        int sum = nums[0];
        int minLength = n + 1;
        while (p1 <= n - 1) {
            if (sum >= target) {
                minLength = Math.min(p2 - p1 + 1, minLength);
                sum -= nums[p1];
                p1++;
            } else {
                if (p2 < n - 1) {
                    p2++;
                    sum += nums[p2];
                } else {
                    break;
                }
            }
        }
        return minLength == n + 1 ? 0 : minLength;
    }

    public static void main(String[] args) {
        int nums[] = new int[]{2, 3, 1, 2, 4, 3};
        int target = 7;
        int minLength = new Solution209().minSubArrayLen(target, nums);
        System.out.println(minLength);
    }
}
